home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
smaltalk
/
manchest.lha
/
MANCHESTER
/
manchester
/
2.2
/
Gnats.st
< prev
next >
Wrap
Text File
|
1993-07-24
|
8KB
|
370 lines
" NAME Gnats
AUTHOR TPH@cs.man.ac.uk
FUNCTION flying bugs from LaTeX - simulation/MVC demo
ST-VERSIONS 2.2
PREREQUISITES
CONFLICTS
DISTRIBUTION world
VERSION 1.1
DATE 22 Jan 1989
SUMMARY Gnats
provides a simulation of lots of noxious flying insects found
in gardens on summer evenings, and which also clutter up the LaTeX
manual! A nice little simulation/MVC example. Known to work only
with VI2.2 images, but could probably be made to work with earlier
versions without trouble.(2.2).TPH
"!
'From Smalltalk-80, Version 2.2 of July 4, 1987 on 11 July 1988 at 2:54:31 pm'!
MouseMenuController subclass: #GnatController
instanceVariableNames: ''
classVariableNames: 'GnatYellowButtonMenu GnatYellowButtonMessages '
poolDictionaries: ''
category: 'Simulations-Insects'!
GnatController comment:
'I represent a controller for interaction with a world full of flying insects.
I support a yellow button menu which allows new Gnats to be added
to the world, or an (arbitrary) Gnat removed. I also permit the form,
used by the View to display the Gnats, to be edited.'!
!GnatController methodsFor: 'initialize-release'!
initialize
"Initialize the yellow button menu."
super initialize.
self
yellowButtonMenu: GnatYellowButtonMenu
yellowButtonMessages: GnatYellowButtonMessages! !
!GnatController methodsFor: 'menu messages'!
addGnat
"Add a Gnat to the model."
model add: Gnat new!
editForm
"Edit the form used to display the Gnat."
BitEditor openOnForm: view form scale: 32@32!
removeGnat
"Remove an (arbitrary) Gnat from the model."
model remove! !
!GnatController methodsFor: 'control defaults'!
controlActivity
"Modify the model."
self model move.
super controlActivity!
isControlActive
^(view containsPoint: sensor cursorPoint) & sensor blueButtonPressed not! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
GnatController class
instanceVariableNames: ''!
!GnatController class methodsFor: 'class initialization'!
initialize
"GnatController initialize."
GnatYellowButtonMenu _ PopUpMenu
labels: 'add gnat\remove gnat\edit gnat image' withCRs
lines: #(2).
GnatYellowButtonMessages _ #(addGnat removeGnat editForm).! !
GnatController initialize!
!BitEditor class methodsFor: 'instance creation'!
openOnForm: aForm scale: scaleFactor
^ self openOnForm: aForm
at: (self locateMagnifiedView: aForm scale: scaleFactor) topLeft
scale: scaleFactor! !
View subclass: #GnatView
instanceVariableNames: 'gnatform '
classVariableNames: ''
poolDictionaries: ''
category: 'Simulations-Insects'!
GnatView comment:
'I represent a View on a world full of flying insects. I keep a form
which is used to display each gnat.'!
!GnatView methodsFor: 'initialize-release'!
initialize
"Initialize the receiver. Create a suitable form for display purposes."
super initialize.
gnatform _ (Form
extent: 8@8
fromCompactArray: #('B'
'Ñ'
'Z'
'<'
''
''
'$'
'$'
)
offset: 0@0)! !
!GnatView methodsFor: 'accessing'!
form
"Answer with the form used to display the Gnats."
^gnatform! !
!GnatView methodsFor: 'displaying'!
displayView
"Re-display the entire model."
model asPoints do: [ :eachPoint |
gnatform
displayOn: Display
at: (self displayTransform: eachPoint)
clippingBox: self insetDisplayBox
rule: Form paint
mask: Form black]! !
!GnatView methodsFor: 'controller access'!
defaultControllerClass
^GnatController! !
!GnatView methodsFor: 'updating'!
update: aParameter
"Ignore aParameter, and update the view."
self display! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
GnatView class
instanceVariableNames: ''!
!GnatView class methodsFor: 'instance creation'!
open
"Open a view on an empty GnatWorld."
"GnatView open."
self openOn: GnatWorld new!
openOn: aGnatWorld
"GnatView openOn: (GnatWorld new: 10)."
| topView gnatView |
topView _ StandardSystemView
model: nil
label: aGnatWorld class printString
minimumSize: 300@300.
gnatView _ self new borderWidth: 1.
gnatView model: aGnatWorld.
gnatView window: (0@0 corner: 1000@1000).
gnatView insideColor: Form white.
topView addSubView: gnatView.
topView controller open! !
Model subclass: #GnatWorld
instanceVariableNames: 'gnats '
classVariableNames: ''
poolDictionaries: ''
category: 'Simulations-Insects'!
GnatWorld comment:
'I represent a world full of flying insects. I keep an OrderedCollection
of insects. Gnats can be added or removed.'!
!GnatWorld methodsFor: 'initialize-release'!
initialize
gnats _ OrderedCollection new.! !
!GnatWorld methodsFor: 'accessing'!
add: aGnat
"insert aGnat into the world."
gnats add: aGnat!
asPoints
"Answer with an OrderedCollection of (2D) points representing the
receiver."
^gnats collect: [ :eachGnat | eachGnat asPoint]!
remove
"Remove an arbitrary Gnat from the world."
gnats isEmpty ifFalse: [gnats removeLast]! !
!GnatWorld methodsFor: 'moving'!
move
"Move all the Gnats in the world."
gnats do: [ :eachGnat | eachGnat move. eachGnat change].
self changed! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
GnatWorld class
instanceVariableNames: ''!
!GnatWorld class methodsFor: 'instance creation'!
new
"Create a new instance of the receiver."
^super new initialize!
new: aNumber
"Create a new instance of the receiver, containing aNumber gnats."
| newWorld |
newWorld _ self new.
aNumber timesRepeat: [newWorld add: Gnat new].
^newWorld! !
Model subclass: #Gnat
instanceVariableNames: 'x y z dx dy dz '
classVariableNames: 'MaxDX MaxDY MaxDZ MaxX MaxY MaxZ RandomGenerator '
poolDictionaries: ''
category: 'Simulations-Insects'!
Gnat comment:
'I represent a class of noxious flying insects, often found in gardens during
summer evenings, and also in LaTeX manuals.
I keep a known location in 3-space, using x, y and z instance variables,
together with a current velocity (dx, dy, dz). Occasionally, I change my
velocity extensively.'!
!Gnat methodsFor: 'initialize-release'!
initialize
"Initialize the receiver."
x _ (RandomGenerator next * MaxX) truncated.
y _ (RandomGenerator next * MaxY) truncated.
z _ (RandomGenerator next * MaxZ) truncated.
dx _ (RandomGenerator next * MaxDX * 2) truncated - MaxDX.
dy _ (RandomGenerator next * MaxDY * 2) truncated - MaxDY.
dz _ (RandomGenerator next * MaxDZ * 2) truncated - MaxDZ.! !
!Gnat methodsFor: 'accessing'!
asPoint
"Answer with a 2D point representing the x and y coordinates
of the receiver."
^x@y!
dx: aNumber
"Set the x speed of the receiver."
dx _ aNumber!
dy: aNumber
"Set the y speed of the receiver."
dy _ aNumber!
dz: aNumber
"Set the z speed of the receiver."
dz _ aNumber!
x: aNumber
"Set the x coordinate of receiver."
x _ aNumber!
y: aNumber
"Set the y coordinate of receiver."
y _ aNumber!
z: aNumber
"Set the z coordinate of receiver."
z _ aNumber! !
!Gnat methodsFor: 'moving'!
change
"Only change speed and direction with low probability."
(RandomGenerator next > 0.9) ifTrue: [self changeDirection].!
changeDirection
"Change the current deltas."
dx _ (RandomGenerator next * 2 * MaxDX) truncated - MaxDX.
dy _ (RandomGenerator next * 2 * MaxDY) truncated - MaxDY.
dz _ (RandomGenerator next * 2 * MaxDZ) truncated - MaxDZ.!
move
"Move the receiver to a new location as given by the current
deltas. Simulate a closed universe."
x _ x + dx \\ MaxX.
y _ y + dy \\ MaxY.
z _ z + dz \\ MaxZ.! !
!Gnat methodsFor: 'printing'!
printOn: aStream
aStream nextPutAll: self class printString, ' at ('.
x printOn: aStream.
aStream nextPut: Character space.
y printOn: aStream.
aStream nextPut: Character space.
z printOn: aStream.
aStream nextPut: $).! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
Gnat class
instanceVariableNames: ''!
!Gnat class methodsFor: 'instance creation'!
new
"Create a new initialized instance of the receiver."
^super new initialize! !
!Gnat class methodsFor: 'class initialization'!
initialize
"Gnat initialize."
MaxX _ MaxY _ MaxZ _ 1000.
MaxDX _ MaxDY _ MaxDZ _ 50.
RandomGenerator _ Random new.! !
Gnat initialize!